package com.qhhc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qhhc.entity.CarFeature;
import com.qhhc.service.impl.CarFeatureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/car-features")
public class CarFeatureController {
    @Autowired
    private CarFeatureService carFeatureService;

    // 1. 基本查询示例
    @GetMapping("/basic")
    public ResponseEntity<List<CarFeature>> basicQuery() {
        QueryWrapper<CarFeature> wrapper = new QueryWrapper<>();
        wrapper.select("feature_id", "feature_name", "category")
              .eq("status", 1)
              .orderByDesc("create_date");
        return ResponseEntity.ok(carFeatureService.list(wrapper));
    }

    // 2. Lambda查询示例
    @GetMapping("/lambda")
    public ResponseEntity<List<CarFeature>> lambdaQuery() {
        LambdaQueryWrapper<CarFeature> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(CarFeature::getFeatureId, CarFeature::getFeatureName, CarFeature::getCategory)
              .eq(CarFeature::getStatus, 1)
              .orderByDesc(CarFeature::getCreateDate);
        return ResponseEntity.ok(carFeatureService.list(wrapper));
    }

    // 3. 模糊查询示例
    @GetMapping("/like")
    public ResponseEntity<List<CarFeature>> likeQuery(@RequestParam String keyword) {
        QueryWrapper<CarFeature> wrapper = new QueryWrapper<>();
        wrapper.like("feature_name", keyword)
              .or()
              .like("category", keyword);
        return ResponseEntity.ok(carFeatureService.list(wrapper));
    }

    // 4. 范围查询示例
    @GetMapping("/between")
    public ResponseEntity<List<CarFeature>> betweenQuery(@RequestParam String startDate, @RequestParam String endDate) {
        QueryWrapper<CarFeature> wrapper = new QueryWrapper<>();
        wrapper.between("create_date", startDate, endDate);
        return ResponseEntity.ok(carFeatureService.list(wrapper));
    }

    // 5. 分组查询示例
    @GetMapping("/group")
    public ResponseEntity<List<Map<String, Object>>> groupQuery() {
        QueryWrapper<CarFeature> wrapper = new QueryWrapper<>();
        wrapper.select("category", "count(*) as count")
              .groupBy("category");
        return ResponseEntity.ok(carFeatureService.listMaps(wrapper));
    }

    // 6. 分页查询示例
    @GetMapping("/page")
    public ResponseEntity<IPage<CarFeature>> pageQuery(
            @RequestParam(defaultValue = "2") Integer current,
            @RequestParam(defaultValue = "2") Integer size) {
        Page<CarFeature> page = new Page<>(current, size);
        QueryWrapper<CarFeature> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_date");
        return ResponseEntity.ok(carFeatureService.page(page, wrapper));
    }

    // 7. 更新条件构造器示例
    @PutMapping("/update")
    public ResponseEntity<Boolean> updateQuery(@RequestBody CarFeature carFeature) {
        if (carFeature == null) {
            return ResponseEntity.badRequest().body(false);
        }
        UpdateWrapper<CarFeature> wrapper = new UpdateWrapper<>();
        wrapper.eq("feature_id", carFeature.getFeatureId());
        if (carFeature.getFeatureName() != null) {
            wrapper.set("feature_name", carFeature.getFeatureName());
        }
        if (carFeature.getCategory() != null) {
            wrapper.set("category", carFeature.getCategory());
        }
        return ResponseEntity.ok(carFeatureService.update(wrapper));
    }

    // 8. Lambda更新条件构造器示例
    @PutMapping("/lambda-update")
    public ResponseEntity<Boolean> lambdaUpdateQuery(@RequestBody CarFeature carFeature) {
        if (carFeature == null) {
            return ResponseEntity.badRequest().body(false);
        }
        LambdaUpdateWrapper<CarFeature> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CarFeature::getFeatureId, carFeature.getFeatureId());
        if (carFeature.getFeatureName() != null) {
            wrapper.set(CarFeature::getFeatureName, carFeature.getFeatureName());
        }
        if (carFeature.getCategory() != null) {
            wrapper.set(CarFeature::getCategory, carFeature.getCategory());
        }
        return ResponseEntity.ok(carFeatureService.update(wrapper));
    }

    // 9. 复杂条件查询示例
    @GetMapping("/complex")
    public ResponseEntity<List<CarFeature>> complexQuery(
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Boolean isStandard,
            @RequestParam(required = false) Integer status) {
        QueryWrapper<CarFeature> wrapper = new QueryWrapper<>();
        wrapper.eq(category != null, "category", category)
              .eq(isStandard != null, "is_standard", isStandard)
              .eq(status != null, "status", status)
              .orderByDesc("create_date");
        return ResponseEntity.ok(carFeatureService.list(wrapper));
    }

    // 10. 子查询示例
    @GetMapping("/sub-query")
    public ResponseEntity<List<CarFeature>> subQuery() {
        QueryWrapper<CarFeature> wrapper = new QueryWrapper<>();
        wrapper.inSql("feature_id", "SELECT feature_id FROM car_feature WHERE status = 1");
        return ResponseEntity.ok(carFeatureService.list(wrapper));
    }

    // 11. 原生SQL查询示例
    @GetMapping("/raw-sql")
    public ResponseEntity<List<Map<String, Object>>> rawSqlQuery() {
        QueryWrapper<CarFeature> wrapper = new QueryWrapper<>();
        wrapper.apply("date_format(create_date,'%Y-%m-%d') = {0}", "2025-04-09");
        return ResponseEntity.ok(carFeatureService.listMaps(wrapper));
    }

    // 12. 链式查询示例
    @GetMapping("/chain")
    public ResponseEntity<List<CarFeature>> chainQuery() {
        List<CarFeature> list = carFeatureService.lambdaQuery()
                .eq(CarFeature::getStatus, 1)
                .like(CarFeature::getFeatureName, "激光")
                .orderByDesc(CarFeature::getCreateDate)
                .list();
        return ResponseEntity.ok(list);
    }

    // 13. 统计查询示例
    @GetMapping("/count")
    public ResponseEntity<Long> countQuery() {
        QueryWrapper<CarFeature> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        return ResponseEntity.ok(carFeatureService.count(wrapper));
    }

    // 14. 批量更新示例
    @PutMapping("/batch-update")
    public ResponseEntity<Boolean> batchUpdate(@RequestBody List<CarFeature> features) {
        return ResponseEntity.ok(carFeatureService.updateBatchById(features));
    }

    // 15. 批量删除示例
    @DeleteMapping("/batch-delete")
    public ResponseEntity<Boolean> batchDelete(@RequestBody List<Long> ids) {
        return ResponseEntity.ok(carFeatureService.removeByIds(ids));
    }

    @GetMapping("/{featureId}")
    public ResponseEntity<CarFeature> getCarFeatureById(@PathVariable Long featureId) {
        return ResponseEntity.ok(carFeatureService.getById(featureId));
    }

    @PostMapping("/getAllFeatures")
    public ResponseEntity<List<CarFeature>> getAllCarFeatures() {
        QueryWrapper<CarFeature> wrapper = new QueryWrapper();
        wrapper.eq("status", 1);
        List<CarFeature> list = carFeatureService.list(wrapper);
        return ResponseEntity.ok(list);
    }

    @DeleteMapping("/delete/{featureId}")
    public ResponseEntity<Boolean> deleteCarFeatureId(@PathVariable Long featureId) {
        boolean b = carFeatureService.removeById(featureId);
        return ResponseEntity.ok(b);
    }

    @PostMapping("/createOrUpdate")
    public ResponseEntity<Boolean> createOrUpdateCarFeature(@RequestBody CarFeature carFeature) {
        boolean save = carFeatureService.saveOrUpdate(carFeature);
        return ResponseEntity.ok(save);
    }
}
